var isAnyFileUploaded = false;
//$("#someDiv").show();

var UploadedFilesArr = [];
var uploaderFileID = "";
var uploadStatusFromServerResponse = "";
var fileuploadrequest_c = 0;

 $(function() {
    $("label").click(function() {
        //  alert("label click");
        $("#fileselect").click();
    });
});

function bytesToSize(bytes, precision) {
    var kilobyte = 1024;
    var megabyte = kilobyte * 1024;
    var gigabyte = megabyte * 1024;
    var terabyte = gigabyte * 1024;

    if ((bytes >= 0) && (bytes < kilobyte)) {
        return bytes + ' bytes';

    } else if ((bytes >= kilobyte) && (bytes < megabyte)) {
        return (bytes / kilobyte).toFixed(precision) + ' KB';

    } else if ((bytes >= megabyte) && (bytes < gigabyte)) {
        return (bytes / megabyte).toFixed(precision) + ' MB';

    } else if ((bytes >= gigabyte) && (bytes < terabyte)) {
        return (bytes / gigabyte).toFixed(precision) + ' GB';

    } else if (bytes >= terabyte) {
        return (bytes / terabyte).toFixed(precision) + ' TB';

    } else {
        return bytes + ' bytes';
    }
}


function addUploadProcessingRow(fsize, fid, fname) {
    var isFurtherProced = "true";
    var UPLOAD_MAX_LIMIT = 5242880; //5 mb

    var fileSize = fsize;

    if (!isNaN(fsize)) {
        fsize = bytesToSize(fsize, 2);
    }

    var lirec = "<li id=\"" + fid + "\" class=\"uploading\">";
    lirec += "<table width='100%'>";
    lirec += "<tr>";
    lirec += "<td width='80%' align='left'>";
    lirec += "<div class=\"plupload_file_thumb\" style='padding:5px'><img src='" + approotpath + "Content/images/20X20gif.gif' width='55px'/></div>";
    lirec += "<div class=\"plupload_file_name\" ><span>" + fname + "</span></div>";
    lirec += "</td>";
    //lirec += "<td width='20%' align='left'>";
    //lirec += "<div class=\"plupload_file_size\"><span  id=\"" + fid + "_size\">" + fsize + "</span></div>";
    //lirec += "</td>";
    lirec += "<td width='20%' align='left'>";
    lirec += "<div class=\"plupload_file_action\" id=\"" + fid + "_emsg\" ></div>";
    lirec += "</td>";
    lirec += "</tr>";
    lirec += "</table>";
    lirec += "</li>";

    $('#html5_uploader_filelist').append(lirec).addClass('');


    //if (UPLOAD_MAX_LIMIT < fileSize) {

    //    isFurtherProced = "false";
    //    setResponseOnUploadProgress('file_size_too_large',fid);
    //}
    //else {
        var responseTextVal = "uploading..";
        $("#" + fid + "_emsg").html("<a href=\"javascript:void(0);\" title=\"" + responseTextVal + "\" style=\"display: block;\" class='upload-progress' onMouseHover=\"return false;\">&nbsp;</a>");
    //}
    return isFurtherProced;
}

function expireUserActivity(activityType) {

    if (activityType == "sharing" || activityType == "copy") {
        alert("Session Expired");
        //lightbox_closeCurrent();
        //clearSharingProgress();
    }
    else if (activityType == "download") {
        alert("Session Expired");
//        lightbox_closeCurrent();

    }
    else if (activityType == "uploading") {
        alert("Session Expired");
        //lightbox_closeCurrent();

    }
    window.location.href = 'index.php';    
    return false;
}

function setResponseOnUploadProgress(resJson) {

    //alert("setResponseOnUploadProgress: res=> "+responseTextVal+" for: "+requestedFileResponseForFile);
    var reqFileResponse = "";
    var reqFileSize = 0;
    var requestedFileResponseForFile = "";
    var uploadPath = "";
    var uploadname = "";

    var msgJson = $.parseJSON(resJson.msgJson);
    requestedFileResponseForFile = msgJson.lid;
    reqFileResponse = msgJson.result;
    reqFileSize = msgJson.size;
    uploadPath = msgJson.path;
    uploadname = msgJson.fname;

    var sUploadPathDisplay = approotpath + (uploadPath.replace("~/", ""));

    var inputhidden = "<input type='hidden' class='pimgid' value='" + (parseInt(msgJson.pimgid) > 0 ? msgJson.pimgid : 0) + "' />";
    if (requestedFileResponseForFile.indexOf("session_failed") >= 0) {
        return expireUserActivity("uploading");
    }

    if (document.getElementById(requestedFileResponseForFile)) {
        $("#" + requestedFileResponseForFile + "_emsg").html("<a href=\"javascript:void(0);\" title=\"" + reqFileResponse + "\" style=\"display: block;\">&nbsp;" + inputhidden + "</a>");
        if (reqFileSize > 0 && document.getElementById(requestedFileResponseForFile + "_size")) {
            $("#" + requestedFileResponseForFile + "_size").html(reqFileSize);
        }

        if (parseInt(resJson.errCode) == 250) {
            isAnyFileUploaded = true;
            $("#" + requestedFileResponseForFile).attr("class", "upload-success");
            $("#" + requestedFileResponseForFile).find(".plupload_file_thumb").find("img").attr("src", sUploadPathDisplay);
            
            $("#" + requestedFileResponseForFile).find("#" + requestedFileResponseForFile + "_emsg").attr("class", "upload-done");
        }
        else {
            $("#" + requestedFileResponseForFile).attr("class", "upload-failure");
            $("#" + requestedFileResponseForFile).find(".plupload_file_thumb").find("img").attr("src", approotpath + "Content/images/404.png");
            $("#" + requestedFileResponseForFile).find("#" + requestedFileResponseForFile + "_emsg").attr("class", "upload-failed");
        }
        //$("#" + requestedFileResponseForFile).find("#" + requestedFileResponseForFile + "_emsg").attr("onClick", "deleteImage(this);");
    }
    else {
        alert("DOM Element: " + requestedFileResponseForFile + " is missing");

    }

}


function bindImages(resJson) {

    //alert("setResponseOnUploadProgress: res=> "+responseTextVal+" for: "+requestedFileResponseForFile);
    var reqFileResponse = "";
    var reqFileSize = 0;
    var requestedFileResponseForFile = "";
    var uploadPath = "";
    var uploadname = "";

    var msgJson = $.parseJSON(resJson.msgJson);
    var vcount=0;
    for (vcount = 0; vcount < msgJson.length; vcount++) {
        requestedFileResponseForFile = "uploadc-" + (++fileuploadrequest_c);
        reqFileResponse = "Delete?";
        reqFileSize = 0;
        uploadPath = msgJson[vcount].sImagePath;
        uploadname = uploadPath.substring(uploadPath.lastIndexOf('/') + 1);

        addUploadProcessingRow(reqFileSize, requestedFileResponseForFile, uploadname);
        var sUploadPathDisplay = approotpath + (uploadPath.replace("~/", ""));

        var inputhidden = "<input type='hidden' class='pimgid' value='" + (parseInt(msgJson[vcount].nPImgid) > 0 ? msgJson[vcount].nPImgid : 0) + "' />";
        if (document.getElementById(requestedFileResponseForFile)) {
            $("#" + requestedFileResponseForFile).attr("class", "img-" + parseInt(msgJson[vcount].nPImgid));
            $("#" + requestedFileResponseForFile + "_emsg").html("<a href=\"javascript:void(0);\" title=\"" + reqFileResponse + "\" style=\"display: block;\">&nbsp;" + inputhidden + "</a>");
            if (reqFileSize > 0 && document.getElementById(requestedFileResponseForFile + "_size")) {
                $("#" + requestedFileResponseForFile + "_size").html(reqFileSize);
            }
            $("#" + requestedFileResponseForFile).find(".plupload_file_thumb").find("img").attr("src", sUploadPathDisplay);
            $("#" + requestedFileResponseForFile).find("#" + requestedFileResponseForFile + "_emsg").attr("class", "upload-failed");
            $("#" + requestedFileResponseForFile).find("#" + requestedFileResponseForFile + "_emsg").attr("onClick", "deleteImage(this);");
        }
    }

}


function getInternetExplorerVersion()  
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

function A(fileid) {
 
    var oas = new ActiveXObject("Scripting.FileSystemObject");
    var d = document.getElementById(fileid).value;
    var e = oas.getFile(d);
    var f = e.size;
    //alert(f + " bytes");
}

var ver = getInternetExplorerVersion();

